<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span class="hs-pragma">{-# LANGUAGE CPP #-}</span><span>
</span><span id="line-2"></span><span>
</span><span id="line-3"></span><span class="hs-comment">-- | Note [Base Dir]</span><span>
</span><span id="line-4"></span><span class="hs-comment">-- ~~~~~~~~~~~~~~~~~</span><span>
</span><span id="line-5"></span><span class="hs-comment">--</span><span>
</span><span id="line-6"></span><span class="hs-comment">-- GHC's base directory or top directory containers miscellaneous settings and</span><span>
</span><span id="line-7"></span><span class="hs-comment">-- the package database.  The main compiler of course needs this directory to</span><span>
</span><span id="line-8"></span><span class="hs-comment">-- read those settings and read and write packages. ghc-pkg uses it to find the</span><span>
</span><span id="line-9"></span><span class="hs-comment">-- global package database too.</span><span>
</span><span id="line-10"></span><span class="hs-comment">--</span><span>
</span><span id="line-11"></span><span class="hs-comment">-- In the interest of making GHC builds more relocatable, many settings also</span><span>
</span><span id="line-12"></span><span class="hs-comment">-- will expand `${top_dir}` inside strings so GHC doesn't need to know it's on</span><span>
</span><span id="line-13"></span><span class="hs-comment">-- installation location at build time. ghc-pkg also can expand those variables</span><span>
</span><span id="line-14"></span><span class="hs-comment">-- and so needs the top dir location to do that too.</span><span>
</span><span id="line-15"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">GHC.BaseDir</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-16"></span><span>
</span><span id="line-17"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../base/src/Prelude.html#"><span class="hs-identifier">Prelude</span></a></span><span> </span><span class="hs-comment">-- See Note [Why do we import Prelude here?]</span><span>
</span><span id="line-18"></span><span>
</span><span id="line-19"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../base/src/Data.List.html#"><span class="hs-identifier">Data.List</span></a></span><span>
</span><span id="line-20"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../filepath/src/System.FilePath.html#"><span class="hs-identifier">System.FilePath</span></a></span><span>
</span><span id="line-21"></span><span>
</span><span id="line-22"></span><span class="hs-comment">-- Windows</span><span class="hs-cpp">
#if defined(mingw32_HOST_OS)
</span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../base/src/System.Environment.html#"><span class="hs-identifier">System.Environment</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../base/src/System.Environment.ExecutablePath.html#getExecutablePath"><span class="hs-identifier">getExecutablePath</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-25"></span><span class="hs-comment">-- POSIX</span><span class="hs-cpp">
#elif defined(darwin_HOST_OS) || defined(linux_HOST_OS) || defined(freebsd_HOST_OS)
</span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">System.Environment</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">getExecutablePath</span><span class="hs-special">)</span><span class="hs-cpp">
#endif
</span><span>
</span><span id="line-30"></span><span class="hs-comment">-- | Expand occurrences of the @$topdir@ interpolation in a string.</span><span>
</span><span id="line-31"></span><span class="annot"><a href="GHC.BaseDir.html#expandTopDir"><span class="hs-identifier hs-type">expandTopDir</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="../../base/src/GHC.IO.html#FilePath"><span class="hs-identifier hs-type">FilePath</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../base/src/GHC.Base.html#String"><span class="hs-identifier hs-type">String</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../base/src/GHC.Base.html#String"><span class="hs-identifier hs-type">String</span></a></span><span>
</span><span id="line-32"></span><span id="expandTopDir"><span class="annot"><span class="annottext">expandTopDir :: FilePath -&gt; FilePath -&gt; FilePath
</span><a href="GHC.BaseDir.html#expandTopDir"><span class="hs-identifier hs-var hs-var">expandTopDir</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">FilePath -&gt; FilePath -&gt; FilePath -&gt; FilePath
</span><a href="GHC.BaseDir.html#expandPathVar"><span class="hs-identifier hs-var">expandPathVar</span></a></span><span> </span><span class="annot"><span class="annottext">FilePath
</span><span class="hs-string">&quot;topdir&quot;</span></span><span>
</span><span id="line-33"></span><span>
</span><span id="line-34"></span><span class="hs-comment">-- | @expandPathVar var value str@</span><span>
</span><span id="line-35"></span><span class="hs-comment">--</span><span>
</span><span id="line-36"></span><span class="hs-comment">--   replaces occurrences of variable @$var@ with @value@ in str.</span><span>
</span><span id="line-37"></span><span class="annot"><a href="GHC.BaseDir.html#expandPathVar"><span class="hs-identifier hs-type">expandPathVar</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="../../base/src/GHC.Base.html#String"><span class="hs-identifier hs-type">String</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../base/src/GHC.IO.html#FilePath"><span class="hs-identifier hs-type">FilePath</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../base/src/GHC.Base.html#String"><span class="hs-identifier hs-type">String</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../base/src/GHC.Base.html#String"><span class="hs-identifier hs-type">String</span></a></span><span>
</span><span id="line-38"></span><span id="expandPathVar"><span class="annot"><span class="annottext">expandPathVar :: FilePath -&gt; FilePath -&gt; FilePath -&gt; FilePath
</span><a href="GHC.BaseDir.html#expandPathVar"><span class="hs-identifier hs-var hs-var">expandPathVar</span></a></span></span><span> </span><span id="local-6989586621679057343"><span class="annot"><span class="annottext">FilePath
</span><a href="#local-6989586621679057343"><span class="hs-identifier hs-var">var</span></a></span></span><span> </span><span id="local-6989586621679057342"><span class="annot"><span class="annottext">FilePath
</span><a href="#local-6989586621679057342"><span class="hs-identifier hs-var">value</span></a></span></span><span> </span><span id="local-6989586621679057341"><span class="annot"><span class="annottext">FilePath
</span><a href="#local-6989586621679057341"><span class="hs-identifier hs-var">str</span></a></span></span><span>
</span><span id="line-39"></span><span>  </span><span class="hs-glyph">|</span><span> </span><span class="annot"><a href="../../base/src/GHC.Maybe.html#Just"><span class="hs-identifier hs-type">Just</span></a></span><span> </span><span id="local-6989586621679057340"><span class="annot"><span class="annottext">FilePath
</span><a href="#local-6989586621679057340"><span class="hs-identifier hs-var">str'</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">FilePath -&gt; FilePath -&gt; Maybe FilePath
forall a. Eq a =&gt; [a] -&gt; [a] -&gt; Maybe [a]
</span><a href="../../base/src/Data.OldList.html#stripPrefix"><span class="hs-identifier hs-var">stripPrefix</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Char
</span><span class="hs-char">'$'</span></span><span class="annot"><span class="annottext">Char -&gt; FilePath -&gt; FilePath
forall a. a -&gt; [a] -&gt; [a]
</span><span class="hs-glyph hs-var">:</span></span><span class="annot"><span class="annottext">FilePath
</span><a href="#local-6989586621679057343"><span class="hs-identifier hs-var">var</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">FilePath
</span><a href="#local-6989586621679057341"><span class="hs-identifier hs-var">str</span></a></span><span>
</span><span id="line-40"></span><span>  </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">FilePath -&gt; Bool
forall (t :: * -&gt; *) a. Foldable t =&gt; t a -&gt; Bool
</span><a href="../../base/src/Data.Foldable.html#null"><span class="hs-identifier hs-var">null</span></a></span><span> </span><span class="annot"><span class="annottext">FilePath
</span><a href="#local-6989586621679057340"><span class="hs-identifier hs-var">str'</span></a></span><span> </span><span class="annot"><span class="annottext">Bool -&gt; Bool -&gt; Bool
</span><span class="hs-operator hs-var">||</span></span><span> </span><span class="annot"><span class="annottext">Char -&gt; Bool
</span><a href="../../filepath/src/System.FilePath.Windows.html#isPathSeparator"><span class="hs-identifier hs-var">isPathSeparator</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">FilePath -&gt; Char
forall a. [a] -&gt; a
</span><a href="../../base/src/GHC.List.html#head"><span class="hs-identifier hs-var">head</span></a></span><span> </span><span class="annot"><span class="annottext">FilePath
</span><a href="#local-6989586621679057340"><span class="hs-identifier hs-var">str'</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-41"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">FilePath
</span><a href="#local-6989586621679057342"><span class="hs-identifier hs-var">value</span></a></span><span> </span><span class="annot"><span class="annottext">FilePath -&gt; FilePath -&gt; FilePath
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="../../base/src/GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">FilePath -&gt; FilePath -&gt; FilePath -&gt; FilePath
</span><a href="GHC.BaseDir.html#expandPathVar"><span class="hs-identifier hs-var">expandPathVar</span></a></span><span> </span><span class="annot"><span class="annottext">FilePath
</span><a href="#local-6989586621679057343"><span class="hs-identifier hs-var">var</span></a></span><span> </span><span class="annot"><span class="annottext">FilePath
</span><a href="#local-6989586621679057342"><span class="hs-identifier hs-var">value</span></a></span><span> </span><span class="annot"><span class="annottext">FilePath
</span><a href="#local-6989586621679057340"><span class="hs-identifier hs-var">str'</span></a></span><span>
</span><span id="line-42"></span><span class="annot"><a href="GHC.BaseDir.html#expandPathVar"><span class="hs-identifier hs-var">expandPathVar</span></a></span><span> </span><span id="local-6989586621679057334"><span class="annot"><span class="annottext">FilePath
</span><a href="#local-6989586621679057334"><span class="hs-identifier hs-var">var</span></a></span></span><span> </span><span id="local-6989586621679057333"><span class="annot"><span class="annottext">FilePath
</span><a href="#local-6989586621679057333"><span class="hs-identifier hs-var">value</span></a></span></span><span> </span><span class="hs-special">(</span><span id="local-6989586621679057332"><span class="annot"><span class="annottext">Char
</span><a href="#local-6989586621679057332"><span class="hs-identifier hs-var">x</span></a></span></span><span class="annot"><span class="hs-glyph hs-type">:</span></span><span id="local-6989586621679057331"><span class="annot"><span class="annottext">FilePath
</span><a href="#local-6989586621679057331"><span class="hs-identifier hs-var">xs</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Char
</span><a href="#local-6989586621679057332"><span class="hs-identifier hs-var">x</span></a></span><span> </span><span class="annot"><span class="annottext">Char -&gt; FilePath -&gt; FilePath
forall a. a -&gt; [a] -&gt; [a]
</span><span class="hs-glyph hs-var">:</span></span><span> </span><span class="annot"><span class="annottext">FilePath -&gt; FilePath -&gt; FilePath -&gt; FilePath
</span><a href="GHC.BaseDir.html#expandPathVar"><span class="hs-identifier hs-var">expandPathVar</span></a></span><span> </span><span class="annot"><span class="annottext">FilePath
</span><a href="#local-6989586621679057334"><span class="hs-identifier hs-var">var</span></a></span><span> </span><span class="annot"><span class="annottext">FilePath
</span><a href="#local-6989586621679057333"><span class="hs-identifier hs-var">value</span></a></span><span> </span><span class="annot"><span class="annottext">FilePath
</span><a href="#local-6989586621679057331"><span class="hs-identifier hs-var">xs</span></a></span><span>
</span><span id="line-43"></span><span class="annot"><a href="GHC.BaseDir.html#expandPathVar"><span class="hs-identifier hs-var">expandPathVar</span></a></span><span> </span><span class="annot"><span class="annottext">FilePath
</span><span class="hs-identifier">_</span></span><span> </span><span class="annot"><span class="annottext">FilePath
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span>
</span><span id="line-44"></span><span>
</span><span id="line-45"></span><span class="hs-comment">-- | Calculate the location of the base dir</span><span>
</span><span id="line-46"></span><span class="annot"><a href="GHC.BaseDir.html#getBaseDir"><span class="hs-identifier hs-type">getBaseDir</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><span class="hs-identifier hs-type">IO</span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../base/src/GHC.Maybe.html#Maybe"><span class="hs-identifier hs-type">Maybe</span></a></span><span> </span><span class="annot"><a href="../../base/src/GHC.Base.html#String"><span class="hs-identifier hs-type">String</span></a></span><span class="hs-special">)</span><span class="hs-cpp">
#if defined(mingw32_HOST_OS)
</span><span id="getBaseDir"><span class="annot"><span class="annottext">getBaseDir :: IO (Maybe FilePath)
</span><a href="GHC.BaseDir.html#getBaseDir"><span class="hs-identifier hs-var hs-var">getBaseDir</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">FilePath -&gt; Maybe FilePath
forall a. a -&gt; Maybe a
</span><a href="../../base/src/GHC.Maybe.html#Just"><span class="hs-identifier hs-var">Just</span></a></span><span> </span><span class="annot"><span class="annottext">(FilePath -&gt; Maybe FilePath)
-&gt; (FilePath -&gt; FilePath) -&gt; FilePath -&gt; Maybe FilePath
forall b c a. (b -&gt; c) -&gt; (a -&gt; b) -&gt; a -&gt; c
</span><a href="../../base/src/GHC.Base.html#."><span class="hs-operator hs-var">.</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span id="local-6989586621679057328"><span class="annot"><span class="annottext">FilePath
</span><a href="#local-6989586621679057328"><span class="hs-identifier hs-var">p</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">FilePath
</span><a href="#local-6989586621679057328"><span class="hs-identifier hs-var">p</span></a></span><span> </span><span class="annot"><span class="annottext">FilePath -&gt; FilePath -&gt; FilePath
</span><a href="../../filepath/src/System.FilePath.Windows.html#%3C%2F%3E"><span class="hs-operator hs-var">&lt;/&gt;</span></a></span><span> </span><span class="annot"><span class="annottext">FilePath
</span><span class="hs-string">&quot;lib&quot;</span></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">(FilePath -&gt; FilePath)
-&gt; (FilePath -&gt; FilePath) -&gt; FilePath -&gt; FilePath
forall b c a. (b -&gt; c) -&gt; (a -&gt; b) -&gt; a -&gt; c
</span><a href="../../base/src/GHC.Base.html#."><span class="hs-operator hs-var">.</span></a></span><span> </span><span class="annot"><span class="annottext">FilePath -&gt; FilePath
</span><a href="#local-6989586621679057326"><span class="hs-identifier hs-var">rootDir</span></a></span><span> </span><span class="annot"><span class="annottext">(FilePath -&gt; Maybe FilePath) -&gt; IO FilePath -&gt; IO (Maybe FilePath)
forall (f :: * -&gt; *) a b. Functor f =&gt; (a -&gt; b) -&gt; f a -&gt; f b
</span><a href="../../base/src/Data.Functor.html#%3C%24%3E"><span class="hs-operator hs-var">&lt;$&gt;</span></a></span><span> </span><span class="annot"><span class="annottext">IO FilePath
</span><a href="../../base/src/System.Environment.ExecutablePath.html#getExecutablePath"><span class="hs-identifier hs-var">getExecutablePath</span></a></span><span>
</span><span id="line-49"></span><span>  </span><span class="hs-keyword">where</span><span>
</span><span id="line-50"></span><span>    </span><span class="hs-comment">-- locate the &quot;base dir&quot; when given the path</span><span>
</span><span id="line-51"></span><span>    </span><span class="hs-comment">-- to the real ghc executable (as opposed to symlink)</span><span>
</span><span id="line-52"></span><span>    </span><span class="hs-comment">-- that is running this function.</span><span>
</span><span id="line-53"></span><span>    </span><span class="annot"><a href="#local-6989586621679057326"><span class="hs-identifier hs-type">rootDir</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="../../base/src/GHC.IO.html#FilePath"><span class="hs-identifier hs-type">FilePath</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../base/src/GHC.IO.html#FilePath"><span class="hs-identifier hs-type">FilePath</span></a></span><span>
</span><span id="line-54"></span><span>    </span><span id="local-6989586621679057326"><span class="annot"><span class="annottext">rootDir :: FilePath -&gt; FilePath
</span><a href="#local-6989586621679057326"><span class="hs-identifier hs-var hs-var">rootDir</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">FilePath -&gt; FilePath
</span><a href="../../filepath/src/System.FilePath.Windows.html#takeDirectory"><span class="hs-identifier hs-var">takeDirectory</span></a></span><span> </span><span class="annot"><span class="annottext">(FilePath -&gt; FilePath)
-&gt; (FilePath -&gt; FilePath) -&gt; FilePath -&gt; FilePath
forall b c a. (b -&gt; c) -&gt; (a -&gt; b) -&gt; a -&gt; c
</span><a href="../../base/src/GHC.Base.html#."><span class="hs-operator hs-var">.</span></a></span><span> </span><span class="annot"><span class="annottext">FilePath -&gt; FilePath
</span><a href="../../filepath/src/System.FilePath.Windows.html#takeDirectory"><span class="hs-identifier hs-var">takeDirectory</span></a></span><span> </span><span class="annot"><span class="annottext">(FilePath -&gt; FilePath)
-&gt; (FilePath -&gt; FilePath) -&gt; FilePath -&gt; FilePath
forall b c a. (b -&gt; c) -&gt; (a -&gt; b) -&gt; a -&gt; c
</span><a href="../../base/src/GHC.Base.html#."><span class="hs-operator hs-var">.</span></a></span><span> </span><span class="annot"><span class="annottext">FilePath -&gt; FilePath
</span><a href="../../filepath/src/System.FilePath.Windows.html#normalise"><span class="hs-identifier hs-var">normalise</span></a></span><span class="hs-cpp">
#elif defined(darwin_HOST_OS) || defined(linux_HOST_OS) || defined(freebsd_HOST_OS)
</span><span class="hs-comment">-- on unix, this is a bit more confusing.</span><span>
</span><span id="line-57"></span><span class="hs-comment">-- The layout right now is something like</span><span>
</span><span id="line-58"></span><span class="hs-comment">--</span><span>
</span><span id="line-59"></span><span class="hs-comment">--   /bin/ghc-X.Y.Z &lt;- wrapper script (1)</span><span>
</span><span id="line-60"></span><span class="hs-comment">--   /bin/ghc       &lt;- symlink to wrapper script (2)</span><span>
</span><span id="line-61"></span><span class="hs-comment">--   /lib/ghc-X.Y.Z/bin/ghc &lt;- ghc executable (3)</span><span>
</span><span id="line-62"></span><span class="hs-comment">--   /lib/ghc-X.Y.Z &lt;- $topdir (4)</span><span>
</span><span id="line-63"></span><span class="hs-comment">--</span><span>
</span><span id="line-64"></span><span class="hs-comment">-- As such, we first need to find the absolute location to the</span><span>
</span><span id="line-65"></span><span class="hs-comment">-- binary.</span><span>
</span><span id="line-66"></span><span class="hs-comment">--</span><span>
</span><span id="line-67"></span><span class="hs-comment">-- getExecutablePath will return (3). One takeDirectory will</span><span>
</span><span id="line-68"></span><span class="hs-comment">-- give use /lib/ghc-X.Y.Z/bin, and another will give us (4).</span><span>
</span><span id="line-69"></span><span class="hs-comment">--</span><span>
</span><span id="line-70"></span><span class="hs-comment">-- This of course only works due to the current layout. If</span><span>
</span><span id="line-71"></span><span class="hs-comment">-- the layout is changed, such that we have ghc-X.Y.Z/{bin,lib}</span><span>
</span><span id="line-72"></span><span class="hs-comment">-- this would need to be changed accordingly.</span><span>
</span><span id="line-73"></span><span class="hs-comment">--</span><span>
</span><span id="line-74"></span><span class="hs-identifier">getBaseDir</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Just</span><span> </span><span class="hs-operator">.</span><span> </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">p</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">p</span><span> </span><span class="hs-operator">&lt;/&gt;</span><span> </span><span class="hs-string">&quot;lib&quot;</span><span class="hs-special">)</span><span> </span><span class="hs-operator">.</span><span> </span><span class="hs-identifier">takeDirectory</span><span> </span><span class="hs-operator">.</span><span> </span><span class="hs-identifier">takeDirectory</span><span> </span><span class="hs-operator">&lt;$&gt;</span><span> </span><span class="hs-identifier">getExecutablePath</span><span class="hs-cpp">
#else
</span><span class="hs-identifier">getBaseDir</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-identifier">Nothing</span><span class="hs-cpp">
#endif
</span></pre></body></html>